package day_2022_9_to_12.Day;

import java.util.HashMap;
import java.util.Map;

/**
 * @author haomin
 * @date 2022/09/06 09:49
 **/
public class Day4_2_Offer57_TwoSum {
    class Solution {
        public int[] twoSum(int[] nums, int target) {
            int l = 0, r = nums.length-1, sum;
            while(l < r){
                sum = nums[l] + nums[r];
                if(sum == target) return new int[]{nums[l], nums[r]};
                else if(sum > target) r--;
                else l++;
            }
            return new int[0];
        }
    }
    class Solution2 {
        public int[] twoSum(int[] nums, int target) {
            Map<Integer, Integer> map = new HashMap<>();
            for(int i = 0; i < nums.length; ++i){
                if(map.containsKey(nums[i])){
                    return new int[]{map.get(nums[i]), nums[i]};
                }
                map.put(target-nums[i], nums[i]);
            }
            return new int[0];
        }
    }
}